/******************************************************************************
 * SAGE - Scalable Adaptive Graphics Environment
 *
 * Module: misc.h
 * Author : Byungil Jeong
 *
 * Copyright (C) 2004 Electronic Visualization Laboratory,
 * University of Illinois at Chicago
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either Version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License along
 * with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Direct questions, comments etc about SAGE to bijeong@evl.uic.edu or 
 * http://www.evl.uic.edu/cavern/forum/
 *
 *****************************************************************************/
 
#ifndef _MISC_H
#define _MISC_H

#define TOKEN_LEN 1024
#define STRBUF_SIZE 1024
#define MAX_TOKENS 10000

#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) (((a)>(b))?(a):(b))
#endif

#include "sageBase.h"

int getToken(char *, char *);
int getToken(char *, char *, char **);
int getToken(FILE *, char *);
int execRemBin(char *ip, char *com);

int sageTcpBlockingSend(int fd, void *buf, int len);
int sageTcpNonBlockingSend(int fd, void *buf, int len);
int sageTcpBlockingRecv(int fd, void *buf, int len);

int getMax2n(int val);
float getPixelSize(sagePixFmt type);
double sageGetTime();
//bool isMutexLocked(pthread_mutex_t *mtx);

namespace sage {
	void usleep(unsigned long usec);
	unsigned int sleep(unsigned int seconds);
	int toupper(char *str);
	int tolower(char *str);
	bool isDataReady(int sockFd);
};

class sageToken {
private:
	char *tokenBuf;
	int idx;
	int strLen;
	
public:
	sageToken(char *str);
	~sageToken();
	
	int getToken(char *token);
};


#ifdef WIN32
struct iovec
{
	u_long iov_len;  
	void FAR* iov_base;
};

struct msghdr
{
 	void	*msg_name;			/* Socket name			*/
 	int		 msg_namelen;		/* Length of name		*/
	struct iovec *msg_iov;		/* Data blocks			*/
 	int		 msg_iovlen;		/* Number of blocks		*/
 	void	*msg_accrights;		/* Per protocol magic (eg BSD file descriptor passing) */ 
 	int		 msg_accrightslen;	/* Length of rights list */
	void	*msg_control;
	int		 msg_controllen;
};
int sendmsg(int s, const struct msghdr *msg, int flags);
int recvmsg(int s, const struct msghdr *msg, int flags);

#endif


#endif
